package ${packageUrl}.controller;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.ga.common.BaseResult;
import ${packageUrl}.dto.${entity}Dto;
import ${packageUrl}.model.${entity};
import ${packageUrl}.service.${entity}Service;
import lombok.extern.slf4j.Slf4j;
/**
 * 
 * @ClassName:  ${entity}Controller  
 * @Description:${title}service层接口
 * @author: ${author}
 * @date:   ${date}
 */
@Slf4j
@Controller
@RequestMapping("/${entityLower}")
public class ${entity}Controller {
	
	@Autowired
	private ${entity}Service ${entityLower}Service;
	
	/**
	   *
	   * @Title: loadPage
	   * @Description: 初始话页面 分页
	   * @param: @param request
	   * @param: @return
	   * @return: BaseResult<?>
	   * @throws
	   */
    @RequestMapping(value = "/loadPage", method = {RequestMethod.POST})
    @ResponseBody
    public BaseResult<?>  loadPage(@RequestBody String request) {
        //JSONObject object = JSONObject.parseObject(request);
        //int pageNo = Integer.parseInt(object.getString("pageNo"));
        // int pageSize =  Integer.parseInt(object.getString("pageSize"));
         //DataSourceDto dto = new DataSourceDto();
         // dto.setPageSize(pageSize);
        //dto.setPageNo(pageNo);
        ${entity}Dto dto = JSON.parseObject(request, ${entity}Dto.class);
        ${entityLower}Service.loadPage(dto);
        JSONObject data = new JSONObject();
        data.put("list",dto.getDataList());
        data.put("total",dto.getTotalRecords());
        data.put("pageNo",pageNo);
        data.put("pageSize",pageSize);
        return BaseResult.success(data);
    }

    /**
     *   
     * @Title: addDataSource
     * @Description:查询不带分页
     * @param: @param request
     * @param: @return
     * @return: BaseResult<?>
     * @throws
     */
    @RequestMapping(value = "/list", method = {RequestMethod.POST})
    @ResponseBody
    public BaseResult<?>  list(@RequestBody String request) {
        
        ${entity}Dto dto = JSON.parseObject(request, ${entity}Dto.class);
        List<${entity}> list  =  ${entityLower}Service.list(dto);
        JSONObject data = new JSONObject();
        data.put("list",list);
        return BaseResult.success(data);
    }

    /**
           * 
     * @Title: insert
     * @Description:插入数据
     * @param: @param request
     * @param: @return
     * @return: BaseResult<?>
     * @throws
     */
    @RequestMapping(value = "/insert", method = {RequestMethod.POST })
    @ResponseBody
    public BaseResult<?> insert(@RequestBody String request) {

        try {
            ${entity} data = JSON.parseObject(request, ${entity}.class);
            ${entityLower}Service.insert(data);
            return BaseResult.success(null);

        } catch (Exception e) {

            log.error("insert error:" + e);
            return BaseResult.fail("fail", e.getMessage());
        }

    }
    
   
    /**
     *
     * @Title: update
     * @Description: 更新操作
     * @param: @param request
     * @param: @return
     * @return: BaseResult<?>
     * @throws
     */
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    @ResponseBody
    public BaseResult<?> update(@RequestBody String request) {

        try {

             ${entity} data = JSON.parseObject(request, ${entity}.class);
             ${entityLower}Service.update(data);
             return BaseResult.success(null);

        } catch (Exception e) {

            log.error("update error:" + e);
            return BaseResult.fail("fail", e.getMessage());
        }

    }

    /**
     * 删除数据源
     * @Title: delete
     * @Description: TODO(描述这个方法的作用)
     * @param: @param request
     * @param: @return
     * @return: BaseResult<?>
     * @throws
     */
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @ResponseBody
    public BaseResult<?> delete(@RequestBody String request) {

        try {

           ${entityLower}Service.delete(JSONObject.parseObject(request).getString("id"));

            return BaseResult.success(null);

        } catch (Exception e) {

            log.error("delete  error:" + e);
            return BaseResult.fail("fail", e.getMessage());
        }

    }

    /**
     * 
     * @Title: queryById   
     * @Description:根据ID查询
     * @param: @param request
     * @param: @return      
     * @return: BaseResult<?>      
     * @throws
     */
    @RequestMapping(value = "/queryById", method = {RequestMethod.POST})
    @ResponseBody
    public BaseResult<?> queryById(@RequestBody String request) {

        try {

            ${entity} data = ${entityLower}Service.queryById(JSONObject.parseObject(request).getString("id"));

            return BaseResult.success(data);

        } catch (Exception e) {

            log.error("queryById  error:" + e);
            return BaseResult.fail("fail", e.getMessage());
        }

    }
	
}
